package czh.cocoa.graphics;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import czh.cocoa.foundation.NSObject;

public class CGImage extends NSObject implements CGDrawable {
	
	private Bitmap mBitmap;
	
	public CGImage(Bitmap bitmap) {
		mBitmap = bitmap;
	}
	
	public CGImage(CGImage image, CGRect rect) {
		int x = Math.round(rect.origin.x);
		int y = Math.round(rect.origin.y);
		int width = Math.round(rect.size.width);
		int height = Math.round(rect.size.height);
		
		if (width > 0 && height > 0) {
			mBitmap = Bitmap.createBitmap(image.bitmap(), x, y, width, height);
		}
	}
	
	public CGImage(CGImage image, float scale) {
		CGSize size = image.size();
		mBitmap = Bitmap.createScaledBitmap(image.bitmap(), 
				(int)(size.width / scale), (int)(size.height / scale), true); 
	}
	
	public void draw(Canvas canvas) {
		if (mBitmap != null) {
			canvas.drawBitmap(mBitmap, 0, 0, null); 
		}
	}

	@Override
	public CGSize size() {
		if (mBitmap != null) {
			return new CGSize(mBitmap.getWidth(), mBitmap.getHeight());  
		}
		return CGSizeMake(0, 0);
	}
	
	Bitmap bitmap() {
		return mBitmap;
	}

}
